<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractKuanListModel;
use app\index\model\ContractKuanModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiGongModel;
use app\index\model\ContractShiModel;
use app\index\model\DeptModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\PiaoTaxListModel;
use app\index\model\PiaoTaxModel;
use app\index\model\StuffRuModel;
use app\index\model\UserModel;
use app\index\relevan\EnginRelevan;
use app\index\service\BudgetService;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PiaoService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Piao extends Common
{
    public function list_piao_kai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','in',[0,1]];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|kai.number|kai.duier|kai.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['kai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['kai.piaotype','=', $param['piaotype']];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kai.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['ratenum']) && !empty($param['ratenum'])){
            $where[] = ['kai.ratenum','like', '%'.$param['ratenum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['kai.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['kai.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['unitaddr']) && !empty($param['unitaddr'])){
            $where[] = ['kai.unitaddr','like', '%'.$param['unitaddr'].'%'];
        }
        if(isset($param['phone']) && !empty($param['phone'])){
            $where[] = ['kai.phone','like', '%'.$param['phone'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kai.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['kai.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
        $data['is_piaotype'] = isset($param['piaotype']) ? $param['piaotype'] : '';


        $data['data'] = PiaokaiModel::alias('kai')
            ->field('kai.*,e.name as enginname')
            ->join('engin e','e.id=kai.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_kai', 'kai'))
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['piaotype'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_kai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入开票金额');
            }
            if(empty($param['duier'])){
                return outputJson(4000, '请选择 对方单位');
            }
            if(empty($param['ratenum'])){
                return outputJson(4000, '请输入纳税人识别号');
            }

            $param['number'] = (new PiaoService())->create_piao_kai_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);



            Db::startTrans();
            try {
                $id = PiaokaiModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['kaiid'] = $id;
                        PiaoKaiListModel::insert($item);
                    }
                }

                (new OptService())->relation_file($files,'piao_kai',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_kai', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_kai', $id, '开票');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
            $data['number'] = (new PiaoService())->create_piao_kai_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = PiaokaiModel::where(['id'=>$param['id']])->find();

            if(empty($param['money'])){
                return outputJson(4000, '请输入开票金额');
            }
            if(empty($param['duier'])){
                return outputJson(4000, '请选择对方单位');
            }
            if(empty($param['ratenum'])){
                return outputJson(4000, '请输入纳税人识别号');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                PiaokaiModel::update($param);

                PiaoKaiListModel::where(['kaiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['kaiid'] = $param['id'];
                        PiaoKaiListModel::insert($item);
                    }
                }

                (new OptService())->relation_file($files,'piao_kai',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_kai', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_kai', $param['id'], '开票');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaokaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $engininfo['shiname'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1])
                ->where('status','in',[0,1])->value('name');
            $data['engininfo'] = $engininfo;



            $list = PiaoKaiListModel::where(['kaiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
            $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function kai_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = PiaokaiModel::where(['id'=>$param['id']])->find();



            if(empty($param['dt'])){
                return outputJson(4000, '请选择开票日期');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                PiaokaiModel::update($param);

                PiaoKaiListModel::where(['kaiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['kaiid'] = $param['id'];
                        PiaoKaiListModel::insert($item);
                    }
                }

                (new OptService())->relation_file($files,'piao_kai',$param['id']);
                (new OptService())->add_flow_log('开票', 'piao_kai', $param['id']);
                (new OptService())->add_log_list('开票', 'piao_kai', $param['id'], '开票');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaokaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['piaotype'] = (new PiaoService())->get_kai_piaotype_name($data['data']['piaotype']);
            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $engininfo['shiname'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1])
                ->where('status','in',[0,1])->value('name');
            $data['engininfo'] = $engininfo;



            $list = PiaoKaiListModel::where(['kaiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_piao_kai(Request $request){
        $param = $request->post();
        $data = PiaokaiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaokaiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_kai', $data['id'], '开票');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_kai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaokaiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['piaotype'] = (new PiaoService())->get_kai_piaotype_name($data['data']['piaotype']);

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $engininfo['shiid'] = 0;
        $engininfo['shiname'] = '';
        $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1])
            ->where('status','in',[0,1])->find();
        if($shiinfo){
            $engininfo['shiid'] = $shiinfo['id'];
            $engininfo['shiname'] = $shiinfo['name'];
        }
        $data['engininfo'] = $engininfo;

        $list = PiaoKaiListModel::where(['kaiid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $acclist = AccountListModel::where(['kaiid'=>$data['data']['id']])->select()->toArray();
        foreach ($acclist as &$itemacc){
            $itemacc['midinfo'] = (new FinanceService())->get_account_list_midinfo($itemacc['type'], $itemacc['mid']);
        }
        $data['acclist'] = $acclist;

        $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_piao_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|shou.number|shou.shouername','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shi']) && !empty($param['shi'])){
            $where[] = ['shou.shi','like', '%'.$param['shi'].'%'];
        }
        if(isset($param['shouername']) && !empty($param['shouername'])){
            $where[] = ['shouer.realname','like', '%'.$param['shouername'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['shouunit']) && !empty($param['shouunit'])){
            $where[] = ['shou.shouunit','=', $param['shouunit']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['shouunit'] = (new UserService())->get_chaninfo_all();
        $data['is_shouunit'] = isset($param['shouunit']) ? $param['shouunit'] : '';


        $data['data'] = PiaoShouModel::alias('shou')
            ->field('shou.*,e.name as enginname,shouer.realname as shouername')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->leftJoin('user shouer','shouer.id=shou.shouer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['shouunit_name'] = DeptModel::get_name_by_pk($item['shouunit']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_shou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_piao_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['shi'])){
                return outputJson(4000, '请输入收票事项');
            }
            if(empty($param['shouunit'])){
                return outputJson(4000, '请选择收票单位');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择收票日期');
            }

            $param['number'] = (new PiaoService())->create_piao_shou_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入发票明细');
            }


            Db::startTrans();
            try {
                $id = PiaoShouModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['shouid'] = $id;
                        PiaoShouListModel::insert($item);
                    }
                }

                (new OptService())->relation_file($files,'piao_shou',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_shou', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_shou', $id, '收票');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['shouer_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');

            $data['piaotype'] = (new PiaoService())->get_shou_piaotype();
            $data['number'] = (new PiaoService())->create_piao_shou_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = PiaoShouModel::where(['id'=>$param['id']])->find();

            if(empty($param['shi'])){
                return outputJson(4000, '请输入收票事项');
            }
            if(empty($param['shouunit'])){
                return outputJson(4000, '请选择收票单位');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择收票日期');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入发票明细');
            }

            Db::startTrans();
            try {
                PiaoShouModel::update($param);

                PiaoShouListModel::where(['shouid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['shouid'] = $param['id'];
                        PiaoShouListModel::insert($item);
                    }
                }

                (new OptService())->relation_file($files,'piao_shou',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_shou', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_shou', $param['id'], '收票');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaoShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['shouunit_info'] = (new UserService())->get_users_part_arr($data['data']['shouunit'], 'dept');
            $data['data']['shouer_info'] = (new UserService())->get_users_part_arr($data['data']['shouer'], 'user');

            $list = PiaoShouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['piaotype_name'] = (new PiaoService())->get_shou_piaotype_name($item['piaotype']);
                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
            }
            $data['list'] = $list;

            $data['piaotype'] = (new PiaoService())->get_shou_piaotype();
            $data['files'] = (new OptService())->get_file('piao_shou', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_piao_shou(Request $request){
        $param = $request->post();
        $data = PiaoShouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaoShouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_shou', $data['id'], '收票');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaoShouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $data['engininfo'] = $engininfo;

        $data['data']['shouunit_name'] = DeptModel::get_name_by_pk($data['data']['shouunit']);
        $data['data']['shouer_name'] = UserModel::get_realname_by_pk($data['data']['shouer']);

        $list = PiaoShouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
            $item['piaotype'] = (new PiaoService())->get_shou_piaotype_name($item['piaotype']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('piao_shou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_piao_tax(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['tax.deleted_user','=', 0];
        $where[] = ['tax.comid','=',$this->userinfo['comid']];
        $where[] = ['tax.isturn','=',1];
        $where[] = ['tax.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|tax.number|tax.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['tax.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['tax.month','like', '%'.$param['month'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['tax.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['tax.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = PiaoTaxModel::alias('tax')
            ->field('tax.*,e.name as enginname')
            ->join('engin e','e.id=tax.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_tax', 'tax'))
            ->order('tax.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('piao_tax');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_piao_tax(Request $request){
        $param = $request->post();
        $data = PiaoTaxModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            PiaoTaxModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'piao_tax', $data['id'], '税费预缴');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_piao_tax(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = PiaoTaxModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $data['engininfo'] = $engininfo;

        $list = PiaoTaxListModel::where(['taxid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['ratetype'] = (new OptService())->get_base_showname('cai_ratetype', $item['ratetype']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('piao_tax', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_piao_kai_hui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','=',1];


        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['kai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['kai.piaotype','=', $param['piaotype']];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kai.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['nums']) && !empty($param['nums'])){
            $where[] = ['list.nums','like', '%'.$param['nums'].'%'];
        }
        if(isset($param['ratenum']) && !empty($param['ratenum'])){
            $where[] = ['kai.ratenum','like', '%'.$param['ratenum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['kai.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['kai.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['unitaddr']) && !empty($param['unitaddr'])){
            $where[] = ['kai.unitaddr','like', '%'.$param['unitaddr'].'%'];
        }
        if(isset($param['phone']) && !empty($param['phone'])){
            $where[] = ['kai.phone','like', '%'.$param['phone'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kai.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['kai.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
        $data['is_piaotype'] = isset($param['piaotype']) ? $param['piaotype'] : '';


        $data['data'] = PiaoKaiListModel::alias('list')
            ->field('list.*,e.name as enginname,kai.piaotype,kai.duier,kai.dt,kai.number')
            ->join('piao_kai kai','kai.id=list.kaiid')
            ->join('engin e','e.id=kai.enginid')
            ->where((new MenuService())->get_miss_where('piao_kai_hui', 'kai'))
            ->where($where)
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['piaotype'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_piao_shou_hui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','=',1];

        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['list.caiid','=', $param['caiid']];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['shou.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['list.piaotype','=', $param['piaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['list.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['nums']) && !empty($param['nums'])){
            $where[] = ['list.nums','like', '%'.$param['nums'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        if(isset($param['shouunit']) && !empty($param['shouunit'])){
            $where[] = ['shou.shouunit','=', $param['shouunit']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['shouunit'] = (new UserService())->get_chaninfo_all();
        $data['is_shouunit'] = isset($param['shouunit']) ? $param['shouunit'] : '';


        $data['piaotype'] = (new PiaoService())->get_shou_piaotype();
        $data['is_piaotype'] = isset($param['piaotype']) ? $param['piaotype'] : '';


        $data['data'] = PiaoShouListModel::alias('list')
            ->field('list.*,e.name as enginname,shou.dt,shou.number,shou.shouunit,cai.name as cainame')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->leftJoin('contract_cai cai','cai.id=list.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou_hui', 'list'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['shouunit_name'] = DeptModel::get_name_by_pk($item['shouunit']);
                $item['piaotype'] = (new PiaoService())->get_shou_piaotype_name($item['piaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function list_piao_engin_hui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.business')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_engin_hui', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['faer'] = '';
                $item['chenger'] = '';
                $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
                if($shiinfo){
                    $item['faer'] = $shiinfo['faer'];
                    $item['chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
                }




                $item['kai_all'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.money');
                $item['kai_nofee'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.fee');
                $item['kai_ratefee'] = PiaoKaiListModel::alias('list')
                    ->join('piao_kai zhu','zhu.id=list.kaiid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.ratefee');


                $item['shou_all'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou zhu','zhu.id=list.shouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->sum('list.money');
                $item['shou_jin'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou zhu','zhu.id=list.shouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['zhu.enginid'=>$item['id']])
                    ->where(['list.piaotype'=>1])
                    ->sum('list.ratefee');

                $item['yingfee'] = round($item['kai_ratefee'] - $item['shou_jin'], 2);
                $item['yingfee'] = $item['yingfee'] < 0 ? 0 : $item['yingfee'];

                $item['taxfee'] = PiaoTaxModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('money');

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


}
